package com.google.code.ojar.sendtemplatemail.app.resendhandler;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileDeleteStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.code.ojar.sendtemplatemail.app.dto.NotSendMail;
import com.google.code.ojar.sendtemplatemail.app.exception.NotSendedMailException;
import com.google.code.ojar.sendtemplatemail.app.library.SendMailLibrary;
import com.google.code.ojar.sendtemplatemail.app.utils.SendMailUtils;
import com.ibm.icu.util.Calendar;

/**
 * Abstract class that define a Handler to resend Mails
 * 
 * @author ottoabreu
 * 
 */
public abstract class ResendHandler {

	private static final Logger logger = LoggerFactory
			.getLogger(ResendHandler.class);

	protected String resendFolder;

	protected ResendHandler() {
		super();
		this.resendFolder = SendMailUtils.getPropertiesString(
				SendMailLibrary.PROPERTY_FILE_NAME,
				SendMailLibrary.PROPERTY_KEY_RESEND_MAIL_FOLDER);
	}

	/**
	 * Generates the not send mail file with all the information of the mail.
	 * 
	 * @param notSend
	 *            {@link NotSendMail} Bean with all the information of the not
	 *            send mail
	 * @throws NotSendedMailException
	 *             if can't generate the file
	 */
	public abstract void generate(NotSendMail notSend)
			throws NotSendedMailException;

	/**
	 * Loads all the not sended mails in order to proceed to re send it
	 * 
	 * @return <{@link java.util.List}<{@link NotSendMail}>
	 * @throws IOException
	 *             if can't read the files
	 * @throws NotSendedMailException
	 *             if an error occurs while generating the list
	 */
	public abstract List<NotSendMail> read() throws IOException,
			NotSendedMailException;

	/**
	 * Deletes all the files with expired policy <br>
	 * Uses {@link FileDeleteStrategy}
	 * 
	 * @param completePath
	 *            String
	 */
	public static void delete(String completePath) {
		try {
			File resendFile = new File(completePath);
			boolean deleted = FileDeleteStrategy.FORCE
					.deleteQuietly(resendFile);
			logger.info("deleted resend file: " + completePath + ": " + deleted);
		} catch (Exception e1) {
			logger.error("Can't delete resend file: " + completePath
					+ " please do it manualy ");
		}
	}

	/**
	 * method that generates the name for a not sended mail file
	 * 
	 * @param fileExt
	 * @param notSend
	 * @return {@link String}
	 */
	protected String getFileName(String fileExt, NotSendMail notSend) {
		StringBuilder nameBuilder = new StringBuilder();
		nameBuilder.append(this.resendFolder);
		nameBuilder.append('/');
		nameBuilder.append(notSend.getMailBean().hashCode());
		nameBuilder.append(Calendar.getInstance().getTimeInMillis());
		nameBuilder.append('.');
		nameBuilder.append(fileExt);
		// String fileName =
		// this.resendFolder+"/"+notSend.getMailBean().hashCode()+Calendar.getInstance().getTimeInMillis()+"."+fileExt;
		notSend.setFileName(nameBuilder.toString());
		return nameBuilder.toString();
	}
	/**
	 * Returns all the files from a folder
	 * @return {@link File}[]
	 */
	protected File[] getFiles(){
		File filesFolder = new File(this.resendFolder);
		File[] allFiles = filesFolder.listFiles();
		return allFiles;
	}

}
